package com.ryan.demo.myio.utils;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;

import com.ryan.demo.resource.utils.ResourceHelper;

public class FileReader {
    public static String readFile(String cat, String fileName) throws Exception{
       String filePath =  ResourceHelper.getFilePath(cat, fileName);
       return readFile(filePath);
    }
    
    public static String readFile(String fileName) throws Exception{
        FileInputStream fis = null;
        try {
            String filePath = ResourceHelper.getPath(fileName);
            fis = new FileInputStream(filePath);

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] receiveBuffer = new byte[1024];
            for (int len = 0; (len = fis.read(receiveBuffer)) != -1;) {
                bos.write(receiveBuffer, 0, len);
            }
            String xml = new String(bos.toByteArray(), "UTF-8");
            return xml;
        } finally{
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
